
from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String, DateTime, func, inspect

# 连接到你的MySQL数据库
engine = create_engine('mysql+pymysql://root:123456@127.0.0.1:3306/a_plus_max?charset=utf8mb4')

# 创建一个MetaData对象来存储数据库的元数据信息
metadata = MetaData()

# 从数据库中反射表结构
users_table = Table('users', metadata, autoload_with=engine)

# 手动创建SQLAlchemy类的定义
class Users:
    def __init__(self, id, username, roles, mobile, created_at, deleted_at, password, updated_at, gender, birthday):
        self.id = id
        self.username = username
        self.roles = roles
        self.mobile = mobile
        self.created_at = created_at
        self.deleted_at = deleted_at
        self.password = password
        self.updated_at = updated_at
        self.gender = gender
        self.birthday = birthday

# 输出 SQLAlchemy 类的定义

# 创建一个检查器对象
inspector = inspect(engine)

# 获取表的索引信息
indexes = inspector.get_indexes('users')

print(f"users = Table('users', metadata,")
for column in users_table.columns:
    col_args = f"Column('{column.name}', {column.type},"
    if column.primary_key:
        col_args += " primary_key=True,"
    if not column.nullable:
        col_args += " nullable=False,"
    if column.default is not None:
        col_args += f" default={column.default.arg},"
    if column.onupdate is not None:
        col_args += f" onupdate={column.onupdate.arg},"
    if column.comment:
        col_args += f" comment='{column.comment}',"
    if any(index['column_names'] == [column.name] for index in indexes):
        col_args += f" index=True,"
    col_args += "),"
    print(f"    {col_args}")
print(")")